var Global = require("../util/global.js");
var websocket = require("../util/websocket.js");
var message = require("../util/message.js");

cc.Class({
    extends: cc.Component,

    properties: {
        blood:100,
        game:null,

        shoe:cc.Prefab,
        power:cc.ProgressBar,
        angle:cc.Label,
        arrow:cc.Sprite,
        bloodLabel:cc.Label,
        bloodBar:cc.ProgressBar,
        flyFlag:false,
        x0:0,
        y0:0,
        v_x:0,
        v_y:0,
        weapon:null,
        angleValue:0,
        powerValue:0,
        powerValueUp:false,
        powerFlag:false,
        shoeList:cc.Node,
        isRobot:false,
    },


    onLoad () {
        this.init();
    },

    start () {

    },

    init(){
        this.canvas = cc.find('Canvas');
        this.game = this.canvas.getComponent('game');

        //箭头坐标
        var arrowWorld = this.canvas.convertToWorldSpaceAR(this.arrow.node.parent);
        this.x0 = arrowWorld.x+this.arrow.node.parent.width/2;
        this.y0 = arrowWorld.y-this.arrow.node.parent.height/2;
        // cc.log(this.x0,this.y0);

        //事件绑定
        // this.canvas.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        // this.canvas.on(cc.Node.EventType.TOUCH_MOVE,this.onTouchMove,this);

        //初始化数据
        this.initData();
    },
    initData(){
        //设置用户
        let u;
        for(let x=0;x<Global.room.userList.length;x++){
            if(Global.room.userList[x].id!=Global.userId){
                u = Global.room.userList[x];
                if(u.robot==true)this.isRobot = true;
                break;
            }
        }
        //初始化血量
        this.setBlood(u.blood);
        //初始化武器
        for(var x=0;x<u.shoeList.length;x++){
            var shoe = cc.instantiate(this.shoe);
            shoe.x = x*shoe.width;
            shoe.id = u.shoeList[x].id;
            this.shoeList.addChild(shoe);
        }
        //挂载武器
        this.initWeapon();
    },
    initWeapon(){
        cc.log("初始化敌方鞋子");

        //鞋子静止状态
        this.flyFlag = false;

        //初始化武器
        if(this.shoeList.childrenCount==0){
            this.game.shoeBothEmpty();
            return;
        }
        if(this.weapon!=null){
            this.weapon.removeFromParent();
        }
        this.weapon = this.shoeList.children[0];
        this.weapon.removeFromParent();
        this.weapon.x = this.node.x+this.weapon.width/2-30;
        this.weapon.y = this.node.y-20;
        this.weapon.anchorY = 0;
        this.weapon.angle = 90;
        this.weapon.group = 'uweapon';
        this.weapon.getComponent(cc.BoxCollider).offset.y = 30;
        this.node.parent.addChild(this.weapon);
        
        //初始化角度
        this.arrow.node.angle = 0;
        this.angle.node.getComponent(cc.Label).string = 0+"°";

        //初始化力量
        this.powerValue = 0;
        this.power.node.getComponent(cc.ProgressBar).progress = this.powerValue;
    },
    setBlood(blood){
        cc.log("设置敌方血量");
        
        this.blood = blood;
        this.bloodLabel.getComponent(cc.Label).string = blood;
        this.bloodBar.getComponent(cc.ProgressBar).progress = blood/100;
    },
    onTouchMove(event){
        //cc.log("调整角度");

        if(!this.game.touchFlag||this.weapon==null)return;
        
        //获取鼠标坐标
        var x = event.getLocationX();
        var y = event.getLocationY();
        // cc.log(x,y);

        //如果在屏幕左侧,返回
        if(x<960/2)return;

        //计算弧度和角度
        var angle = Math.atan2((y-this.y0), (this.x0-x));
        var theta = Math.round(angle*(180/Math.PI));
        
        //角度不允许大于90或者小于0
        if(theta<0)theta=0;
        else if(theta>90)theta=90;
        
        //摆动箭头
        this.arrow.node.angle = theta;
        this.angle.node.getComponent(cc.Label).string = theta+"°";

        //摆动武器
        this.weapon.angle = 90-theta;

        //设置全局弧度
        this.angleValue = angle;

        //设置力量槽可以上下浮动
        if(this.powerFlag==false)this.powerFlag = true;
    },
    onTouchEnd(event){
        if(!this.game.touchFlag||this.weapon==null)return;
        if(this.flyFlag)return;

        //获取鼠标坐标
        var x = event.getLocationX();

        //如果在屏幕左侧,返回
        if(x<960/2)return;

        cc.log("敌方攻击");

        this.powerFlag = false;
        this.flyFlag = true;

        this.v_x = this.powerValue*(700-this.game.wind*20)*Math.cos(this.angleValue);
        this.v_y = this.powerValue*(700-this.game.wind*20)*Math.sin(this.angleValue);
        // cc.log('i',this.v_x+","+this.v_y);
    },
    robotAttack(){
        if(!this.game.touchFlag||this.weapon==null)return;
        if(this.flyFlag)return;

        //cc.log("敌方攻击");

        //随机生成角度并计算弧度
        var theta = Math.ceil(Math.random()*(60-30)+30);
        var angle = theta*(Math.PI/180);
        var power = this.powerValue;

        //摆动箭头
        this.arrow.node.angle = theta;
        this.angle.node.getComponent(cc.Label).string = theta+"°";

        //摆动武器
        this.weapon.angle = 90-theta;

        //设置力量槽可以上下浮动
        if(this.powerFlag==false)this.powerFlag = true;

        //随机发动攻击
        if(power>0.8){
            var i = Math.random()*100;
            if(i<10){
                this.powerFlag = false;
                this.flyFlag = true;
                this.v_x = Math.abs(power*700*Math.cos(angle));
                this.v_y = Math.abs(power*700*Math.sin(angle));
            }
            // cc.log('u',this.v_x,this.v_y);
        }
    },
    responseAttack(content){

        var angle = content.theta*(Math.PI/180);

        //摆动箭头
        this.arrow.node.angle = content.theta;
        this.angle.node.getComponent(cc.Label).string = content.theta+"°";

        //摆动武器
        this.weapon.angle = 90-content.theta;

        //攻击
        this.flyFlag = true;
        this.v_x = Math.abs(content.power*700*Math.cos(angle));
        this.v_y = Math.abs(content.power*700*Math.sin(angle));
    },
    update (dt) {
        if(this.game.touchFlag&&this.powerFlag==true){
            //是否增长力量
            if(this.powerValue<=0){
                this.powerValueUp = true;
            }else if(this.powerValue>=1){
                this.powerValueUp = false;
            }
            //增长或减少力量
            if(this.powerValueUp){
                this.powerValue+=dt;
            }else{
                this.powerValue-=dt;
            }
            this.power.node.getComponent(cc.ProgressBar).progress = this.powerValue;
        }

        if(this.flyFlag==true){
            this.weapon.x-=this.v_x*dt;
            this.v_y -= 600*dt;
            this.weapon.y += this.v_y*dt;
            // cc.log(this.weapon.x+","+this.weapon.y);
            if(this.weapon.y<-500){
                this.weapon = null;
                this.initWeapon();
            }
        }

        //敌方攻击
        if(this.flyFlag==false){
            if(Global.isRobot)this.robotAttack();
        }
    },

    onCollisionEnter: function (other, self) {
        cc.log("被鞋子击中");
        this.blood -= 10;
        if(this.blood>0){
            this.setBlood(this.blood);
        }else{
            this.setBlood(0);
            this.game.win(2);
            this.blood = 100;
        }
        cc.tween(this.node)
            .to(0.05, { scaleX: 0.95 })
            .blink(0.05)
            .to(0.05, { scaleX: 1 })
            .start();
    }
});
